#!/bin/bash

param1=$1
f=$2
#echo "param1 = ${param1}"
len1=${#param1}
len2=`expr $len1 - 2`
#echo $len2
param2=${param1:0:${len2}}
#echo $param2
#echo $f

fun1(){	
	echo -e ""

	if [ $2 ] && [ $2 = "time" ];then
	    echo  "执行<$1>并且计算所需要的花销是: "
	    TIMEFORMAT="" time ./$1
	else
	    echo "执行<$1>程序:"
            ./$1
	fi
	echo -e ""	

}
fun2(){
	gcc -o $1 $2
	if [ -e $1 ];then
	echo "-----------------<$1>文件生成成功-----------------------"
	else
	echo "--------------------文件编译失败-------------------------" 
	echo "--------------------即将退出脚本-------------------------"
	exit -1; 
	fi
}

echo -e ""
echo "-------------------脚本执行开始-------------------------" 
echo "你所执行的程序是：$1"
if [ -e $param2 ];then
	echo "The file is extis! Do you want to rebuild? yes/no"
	read str
	case $str in
	  [yY]|[Yy][eE][sS])	
	    fun2 $param2 $param1
	  ;;
          *)
            echo "No rebuild to action the $param2"
	  ;;
	esac
        fun1 $param2 $f
else
	fun2 $param2 $param1
        fun1 $param2 $f

fi

echo -e ""
echo "测试完成，删除可执行文件 $param2"
echo "-------------------脚本执行完成-------------------------"
rm $param2


